﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Model.Security;
using Cn.Edwon.Spring;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Web.Script;
using Cn.Cellula.Model.Content;
using Cn.Cellula.Client;
using Cn.Edwon.Biz;
using Cn.Edwon.Data;
using System.Data;
using System.Text;
using Cn.Edwon.Web;
using Cn.Cellula.Biz;
namespace Cn.Cellula.Website.Admin.Module.Manhour {
    public partial class View : PageSupport {
        public String JsCode = "null";
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                InitForm();
                LoadData();
            }
        }

        private void LoadData() {
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            try {
                sb.Append("\"User\":" + this.LoadUserData());
            } catch (Exception ex) {
                this.btn_Export.Enabled = false;
                log.Error(ex.Message);
                SiteInfo.Send(Page, TxtDef.ERROR, "获取用户数据异常:" + ex.Message);
                return;
            }
            try {
                sb.Append(",\"Item\":" + this.LoadItemData());
            } catch (Exception ex) {
                this.btn_Export.Enabled = false;
                log.Error(ex.Message);
                SiteInfo.Send(Page, TxtDef.ERROR, "获取项目数据异常:" + ex.Message);
                return;
            }
            try {
                sb.Append(",\"Stat\":" + this.LoadStatData());
            } catch (Exception ex) {
                log.Error(ex.Message);
                this.btn_Export.Enabled = false;
                SiteInfo.Send(Page, TxtDef.ERROR, "获取工时数据异常:" + ex.Message);
                return;
            }
            this.btn_Export.Enabled = true;
            sb.Append("}");
            JsCode = sb.ToString();
        }

        private void InitForm() {
            DateTime _dt = DateTime.Now;
            try {
                _dt = Convert.ToDateTime(Request.QueryString["y"] + "-" + Request.QueryString["m"] + "-1");
            } catch {
                _dt = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-01 0:0:0:")); ;
            }
            this.hid_FromDt.Value = _dt.ToString("yyyy-MM-01 0:0:0");
            this.hid_ToDt.Value = _dt.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59");
            this.lab_title.Text = _dt.ToString("yyyy年MM月工时报表");

        }
        private String LoadUserData() {
            IUserBiz biz = BizFactory.Get<IUserBiz>();
            IList<VSysUserEO> list = new List<VSysUserEO>();
            String hql = "from VSysUserEO eo ";
            hql += " where eo.UserKey<>'" + BizConst.Admin.KEY + "' ";
            hql += " order by eo.RoleLevel desc ";
            list = biz.Find<VSysUserEO>(hql);
            return JsUtil.ParseEOList2Array<VSysUserEO>(list, "UserKey,UserName");
        }

        private String LoadItemData() {
            IItemBiz biz = BizFactory.Get<IItemBiz>();
            String todoRef = SiteConfig.Calendar.TodoRef;
            IList<VItemListEO> list = biz.LoadItemByCategory(todoRef, this.OwnerKey, this.Power);
            return JsUtil.ParseEOList2Array<VItemListEO>(list, "ItemKey,ItemName,CategoryName");
        }

        private String LoadStatData() {
            IBiz biz = BizFactory.Get<IBiz>();
            IList<CallingParameter> ps = new List<CallingParameter>();
            ps.Add(new CallingParameter("r_from_dt", this.hid_FromDt.Value, false));
            ps.Add(new CallingParameter("r_end_dt", this.hid_ToDt.Value, false));
            ps.Add(new CallingParameter("r_users", "", false));
            DataSet ds = biz.ADOExcuteProc("p_stat_project_manhour", ps);
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            if (ds != null && ds.Tables.Count > 0) {
                int idx = 0;
                foreach (DataRowView row in ds.Tables[0].DefaultView) {
                    if (idx > 0)
                        sb.Append(",");
                    sb.Append("\"" + row["item_key"] + row["user_key"] + "\":" + row["man_hour"]);
                    idx++;
                }
            }
            sb.Append("}");
            return sb.ToString();
        }


    }
}